#ifndef _SRC_DFU_AWI_DFU_UNPACK_H_
#define _SRC_DFU_AWI_DFU_UNPACK_H_

#include "awi_dfu_pack.h"

#ifdef __cplusplus
extern "C" {
#endif

#define AWI_DFU_UNPACK_DEF_BUFFER_SIZE 1024

typedef enum {
	AWI_DFU_UNPACK_MAGIC_FIRST,
	AWI_DFU_UNPACK_MAGIC_SECOND,
	AWI_DFU_UNPACK_HEADER,
	AWI_DFU_UNPACK_PAYLOAD,
	AWI_DFU_UNPACK_DONE,
}awi_dfu_unpack_state_t;

typedef struct awi_dfu_unpack awi_dfu_unpack_t;
struct awi_dfu_unpack {
	awi_dfu_unpack_state_t state;
	struct {
		uint16_t version;
		uint16_t dst;
		uint16_t src;
		uint16_t ctrl;
		uint16_t len;
		uint16_t crc;
	}header;
	uint8_t cache_header[AWI_DFU_PROTOCOL_HEADER_LENGTH];
	uint16_t header_pos;
	uint8_t payload[AWI_DFU_UNPACK_DEF_BUFFER_SIZE];
	uint8_t *payload_extra;
	uint16_t payload_extra_length;
	uint16_t payload_pos;
	uint16_t compute_crc;
};

void awi_dfu_unpack_init(awi_dfu_unpack_t *unpack);
void awi_dfu_unpack_clean(awi_dfu_unpack_t *unpack);

void awi_dfu_unpack_reset(awi_dfu_unpack_t *unpack);
int awi_dfu_unpack_feed(awi_dfu_unpack_t *unpack, const uint8_t *buffer, int len, int *consumed);
int awi_dfu_unpack_proc(awi_dfu_unpack_t *unpack, const uint8_t *buffer, int len);

#ifdef __cplusplus
};
#endif
#endif /* _SRC_DFU_AWI_DFU_UNPACK_H_ */